package net.steam.easyjoboa.web.action;

import java.util.List;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import net.steam.easyjoboa.biz.AddressBookBiz;
import net.steam.easyjoboa.biz.AddressBookGroupBiz;
import net.steam.easyjoboa.entity.AddressBook;
import net.steam.easyjoboa.entity.AddressGroup;
import net.steam.easyjoboa.entity.Employee;
import net.steam.easyjoboa.entity.PageBeans;

public class AddressBookAction {
	private AddressBookBiz addressBookBiz;
	private AddressBookGroupBiz addressBookGroupBiz;
	private int pageIndex=1;
	private int pageSize=5;
	private int id;
	private int bookId;
	private PageBeans pageBean;
	private List<AddressBook> list;
	private List<AddressGroup> list2;
	private String name;
	private int groupId=0;
	private int groupsId;
	private long qq=0l;
	private AddressBook addressBook;
	private int[] checkbox;
	Employee employee = (Employee) ServletActionContext.getRequest().getSession().getAttribute("loginUser");
	
	@Action(value="find",results={@Result(name="success",location="../../message/detail.jsp")})
	public String findById(){
		if(pageBean==null){
			pageBean = new PageBeans();
		}
		pageBean.setCurrentPage(pageIndex);
		if(pageSize<=0){
			pageSize=5;
		}
		pageBean.setPageSize(pageSize);
		list=addressBookBiz.finBook(pageBean, id);
		return "success";
	}
	
	@Action(value="pageByGroupId",results={@Result(name="success",location="../../message/personalAddress.jsp")})
	public String pageByGroupId(){
		list2 = addressBookGroupBiz.getAddressGroupByUserId(employee.getUserId());
		if(pageBean==null){
			pageBean = new PageBeans();
		}
		pageBean.setCurrentPage(pageIndex);
		if(pageSize<=0){
			pageSize=5;
		}
		pageBean.setPageSize(pageSize);
		list=addressBookBiz.getBooksByGroupId(pageBean, groupId, name, qq,employee.getUserId());
		return "success";
	}
	
	@Action(value="getPersonal",results={@Result(name="success",location="../../message/personalDetail.jsp")})
	public String getPersonal(){
		addressBook = addressBookBiz.getAddressBook(id);
		return "success";
	}
	
	@Action(value="getUpadatePersonal",results={@Result(name="success",location="../../message/updatePersonal.jsp")})
	public String getUpadatePersonal(){
		list2=addressBookGroupBiz.getAddressGroupByUserId(employee.getUserId());
		addressBook=addressBookBiz.getAddressBook(id);		
		return "success";
	}
	
	@Action(value="updatePersonal",results={@Result(name="success",location="pageByGroupId",type="redirectAction")})
	public String updatePersonal(){
		addressBook.setEmployee(employee);
		addressBook.setId(id);
		addressBookBiz.update(addressBook);
		return "success";
	}

	
	@Action(value="addOrUpdate",results={@Result(name="success",location="pageByGroupId",type="redirectAction")})
	public String update(){

		addressBook=addressBookBiz.getAddressBook(bookId);
		list=addressBookBiz.getListByNameAndGroups(addressBook.getName(),addressBook.getAddressGroup().getAddressGroupId(),employee.getUserId(),addressBook.getId());
		AddressBook addressBooks=list.get(0);
		
		AddressGroup addressGroup =new AddressGroup();
		addressGroup.setAddressGroupId(addressBook.getAddressGroup().getAddressGroupId());
		addressBooks.setName(addressBook.getName());
		addressBooks.setQq(addressBook.getQq());
		addressBooks.setNation(addressBook.getNation());
		addressBooks.setSex(addressBook.getSex());
		addressBooks.setIsmarriage(addressBook.getIsmarriage());
		addressBooks.setOfficePhone(addressBook.getOfficePhone());
		addressBooks.setMobile(addressBook.getMobile());
		addressBooks.setEmployee(employee);
		addressBooks.setAddressGroup(addressGroup);
		addressBooks.setBirthday(addressBook.getBirthday());
		addressBooks.setEmail(addressBook.getEmail());
		addressBooks.setDepartment(null);
		addressBooks.setAddress(addressBook.getAddress());	
		
		addressBookBiz.delete(addressBooks);
		addressBookBiz.update(addressBooks);
		return "success";
	}
	
	@Action(value="addPersonal",results={@Result(name="success",location="pageByGroupId",type="redirectAction"),@Result(name="update",location="../../message/addOrUpdate.jsp"),@Result(name="input",location="../../message/addPersonal.jsp")})
	public String addAddressBook(){
		addressBook.setEmployee(employee);
		try {		
			bookId=addressBookBiz.addBook(addressBook);			
			list=addressBookBiz.getListByNameAndGroups(addressBook.getName(),addressBook.getAddressGroup().getAddressGroupId(),employee.getUserId());
			if(list.size()==2){
				return "update";
			}
			return "success";			
		} catch (Exception e) {
			e.printStackTrace();
			return "input";
		}
		
	}
	@Action(value="deleteBooks",results={@Result(name="success",location="deleteGroup",type="redirectAction")})
	public String del(){
		addressBook=addressBookBiz.getAddressBook(id);
		addressBookBiz.delete(addressBook);
		return "success";
	}
	@Action(value="deleteBook",results={@Result(name="success",location="pageByGroupId",type="redirectAction")})
	public String dels(){
		addressBook=addressBookBiz.getAddressBook(id);
		addressBookBiz.delete(addressBook);
		return "success";
	}
	@Action(value="allDelete",results={@Result(name="success",location="pageByGroupId",type="redirectAction")})
	public String Alldel(){
		for(int i=0;i<checkbox.length;i++){
			addressBook=addressBookBiz.getAddressBook(checkbox[i]);
			addressBookBiz.delete(addressBook);
		}
		return "success";
	}
	
	
	
	public void setAddressBookBiz(AddressBookBiz addressBookBiz) {
		this.addressBookBiz = addressBookBiz;
	}


	public int getPageIndex() {
		return pageIndex;
	}


	public void setPageIndex(int pageIndex) {
		this.pageIndex = pageIndex;
	}


	public int getId() {
		return id;
	}


	public void setId(int id) {
		this.id = id;
	}


	public PageBeans getPageBean() {
		return pageBean;
	}


	public void setPageBean(PageBeans pageBean) {
		this.pageBean = pageBean;
	}


	public List<AddressBook> getList() {
		return list;
	}


	public void setList(List<AddressBook> list) {
		this.list = list;
	}

	public List<AddressGroup> getList2() {
		return list2;
	}

	public void setList2(List<AddressGroup> list2) {
		this.list2 = list2;
	}

	public void setaddressBookGroupBiz(AddressBookGroupBiz addressBookGroupBiz) {
		this.addressBookGroupBiz = addressBookGroupBiz;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public int getGroupId() {
		return groupId;
	}

	public void setGroupId(int groupId) {
		this.groupId = groupId;
	}

	public long getQq() {
		return qq;
	}

	public void setQq(long qq) {
		this.qq = qq;
	}

	public AddressBook getAddressBook() {
		return addressBook;
	}

	public void setAddressBook(AddressBook addressBook) {
		this.addressBook = addressBook;
	}


	public int[] getCheckbox() {
		return checkbox;
	}


	public void setCheckbox(int[] checkbox) {
		this.checkbox = checkbox;
	}

	public int getGroupsId() {
		return groupsId;
	}

	public void setGroupsId(int groupsId) {
		this.groupsId = groupsId;
	}

	public int getPageSize() {
		return pageSize;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

	public int getBookId() {
		return bookId;
	}

	public void setBookId(int bookId) {
		this.bookId = bookId;
	}

	public Employee getEmployee() {
		return employee;
	}

	public void setEmployee(Employee employee) {
		this.employee = employee;
	}

	public void setAddressBookGroupBiz(AddressBookGroupBiz addressBookGroupBiz) {
		this.addressBookGroupBiz = addressBookGroupBiz;
	}	
	
}
